package com.avic.rong.goodsSort.fragment.ts;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.AdvertismentListBean;
import com.avic.rong.BaseFragment;
import com.avic.rong.R;
import com.avic.rong.bean.AdvBean;
import com.avic.rong.goodsSort.activity.GoodsSortActivity;
import com.avic.rong.goodsSort.adapter.GoodsItemRvAdapter;
import com.avic.rong.goodsSort.api.ContentAdvApi;
import com.avic.rong.goodsSort.api.GoodsSortItemApi;
import com.avic.rong.goodsSort.bean.GoodsSortItemBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.GlideUtils;
import com.avic.rong.wedgit.customView.RoundImageView;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.orhanobut.logger.Logger;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class GoodsTuShuFragment extends BaseFragment {

    @BindView(R.id.goods_Ts_Rv)
    RecyclerView goodsTsRv;
    @BindView(R.id.goods_Ts_Img)
    RoundImageView goods_Ts_Img;
    private List<GoodsSortItemBean.InternalListBean> beanList;
    private GoodsItemRvAdapter adapter;
    public int topType = 2;//1:标准,2:图书,3:实用文档,4:软件工具

    public GoodsTuShuFragment() {
        // Required empty public constructor
    }

    public static GoodsTuShuFragment getInstance(int topType) {
        GoodsTuShuFragment tuShuFragment = new GoodsTuShuFragment();
        tuShuFragment.topType = topType;
        return tuShuFragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_goods_tushu, container, false);
        ButterKnife.bind(this, view);
        loadData();
        return view;
    }


    public void loadData() {
        GoodsSortItemApi goodsSortItemApi = new GoodsSortItemApi((GoodsSortActivity) getContext(), delCartListener, topType, "gjb");
        goodsSortItemApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(goodsSortItemApi);
        //广告图
        String avName = "APP-商城-图书分类";
        if (topType == 2) {
            avName = "APP-商城-图书分类";
        } else if (topType == 3) {
            avName = "APP-商城-实用文档分类";
        }
        if (topType == 4) {
            avName = "APP-商城-软件工具分类";
        }
        ContentAdvApi contentAdvApi = new ContentAdvApi((GoodsSortActivity) getActivity(), advListener, avName);
        contentAdvApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(contentAdvApi);
    }

    HttpOnNextListener delCartListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            GoodsSortItemBean bean = (GoodsSortItemBean) subjects.getData();
            Logger.e("cjx", "bz拿到数据");
            beanList = bean.getInternalList();
//            if (!bean.getContent().equals("")){
//                Glide.with(getContext()).load(bean.getContent()).into(goodsTsImg);
//            }
            setAdapter();
        }

        @Override
        public void onCacheNext(String cache) {

        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "bz拿到数据" + e.getMessage());
        }
    };

    //广告
    HttpOnNextListener advListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (ConstantsUtils.STATUS_SUCCESS_200.equals(subjects.getCode())) {
                AdvertismentListBean bean = (AdvertismentListBean) subjects.getData();
                String imgUrl = "";
                if (bean != null && bean.getAdvertismentList().size() > 0) {
                    AdvBean advBean = bean.getAdvertismentList().get(0);
                    imgUrl = advBean.getContent();
                }
                GlideUtils.loaderHanldeRoundImage(getContext(), imgUrl, goods_Ts_Img, R.mipmap.compare_no_pro, 8);
            }
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "bz拿到数据" + e.getMessage());
        }
    };


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void addListener() {

    }

    @Override
    protected int getContentViewId() {
        return 0;
    }

    private void setAdapter() {
        GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
        adapter = new GoodsItemRvAdapter(getContext(), beanList, topType);
        goodsTsRv.setLayoutManager(manager);
        goodsTsRv.setAdapter(adapter);
    }


}
